from flask import Flask,render_template,request,redirect
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/py02db?charset=utf8'
db = SQLAlchemy(app)
class user(db.Model):
    id = db.Column('id',db.Integer,primary_key=True)
    name = db.Column(db.String(9))
    sex = db.Column(db.String(1))
    age = db.Column(db.Integer)
    phone = db.Column(db.String(11))

    def __init__(self, name, sex, age ,phone):
        self.name=name
        self.sex=sex
        self.age = age
        self.phone = phone


@app.route('/index/', methods=['POST','GET'])
@app.route('/', methods=['POST','GET'])
def index():
    if request.method == 'POST':
        name = request.form['uname']
        sex = request.form['usex']
        age = request.form['uage']
        phone = request.form['uphone']
        obj = user(name,sex,int(age),phone)
        db.session.add(obj)
        db.session.commit()
    return redirect('/reveal/')

@app.route('/reveal/', methods=['POST','GET'])
def reveal():
    # rphone = request.args.get('rphone')
    res = user.query.all()
    # if request.method == 'GET':
    #     obj = user.query.filter_by(phone=rphone).first()
    #     if obj:
    #         db.session.delete(obj)
    #         db.session.commit()
    return render_template('reveal.html',res=res)


@app.route('/omit/',methods=['POST','GET'])
def omit():
    rphone = request.args.get('rphone')
    if request.method == 'GET':
        obj = user.query.filter_by(phone=rphone).first()
        if obj:
            db.session.delete(obj)
            db.session.commit()
    return redirect('/reveal/')





if __name__ == '__main__':
    app.run()
